package com.lj.universalframework.utils

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import com.lj.universalframework.app.MyApplication
import com.lj.universalframework.enum.NetworkChanges
import com.lj.universalframework.enum.NetworkStatus
import com.lj.universalframework.enum.NetworkType

/**
 * @ClassName NetworkChangeUtils
 * @Description 网络状态工具类
 * @Author LJ
 * @Date 2022/9/23 16:09
 * @Version 1.0
 */
class NetworkChangeUtils private constructor() {

    companion object {
        val mInstance: NetworkChangeUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            NetworkChangeUtils()
        }
    }


    /**
     * 网络变化
     * networkStatus: 网络连接状态
     * networkType:网络类型
     * networkChanges:网络变化
     */
    private val onNetworkChangesList = arrayListOf<OnNetworkChanges>()


    //当前网络连接状态
    private var networkStatus: NetworkStatus? = null

    //当前网络类型
    private var networkType: NetworkType? = null

    //当前网络变化
    private var networkChanges: NetworkChanges? = null

    /**
     * 获取当前网络连接状态
     * @return NetworkStatus
     */
    fun getNetworkStatus(): NetworkStatus {
        networkStatus?.apply {
            return this
        }
        return if (networkStatus == null) {
            if (isNetworkConnected(MyApplication.mContext)) {
                NetworkStatus.CONNECT
            } else {
                NetworkStatus.DISCONNECT
            }
        } else {
            networkStatus!!
        }
    }

    /**
     * 获取当前网络类型
     * @return NetworkType?
     */
    fun getNetworkType(): NetworkType? {
        return networkType
    }

    /**
     * 获取当前网络变化
     * @return NetworkType?
     */
    fun getNetworkChanges(): NetworkChanges? {
        return networkChanges
    }

    /**
     * 添加回调
     * @param onNetworkChanges
     */
    fun addOnNetworkChanges(onNetworkChanges:OnNetworkChanges) {
        onNetworkChangesList.add(onNetworkChanges)
    }

    /**
     * 移除回调
     * @param onNetworkChanges
     */
    fun removeOnNetworkChanges(onNetworkChanges:OnNetworkChanges) {
        onNetworkChangesList.remove(onNetworkChanges)
    }

    /**
     * 通知更新
     */
    fun notifyUpdate(
        networkStatus: NetworkStatus?,
        networkType: NetworkType?,
        networkChanges: NetworkChanges?
    ) {
        this.networkStatus = networkStatus
        this.networkType = networkType
        this.networkChanges = networkChanges
        onNetworkChangesList.forEach {
            it.onNetworkChangesList(networkStatus, networkType, networkChanges)
        }
    }

    /**
     * 检查网络状态
     */
    private fun isNetworkConnected(context: Context): Boolean {
        var result = false
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cm?.run {
                cm.getNetworkCapabilities(cm.activeNetwork)?.run {
                    result = when {
                        hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
                        hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
                        hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
                        else -> false
                    }
                }
            }
            return result
        }
        cm?.run {
            cm.activeNetworkInfo?.run {
                if (type == ConnectivityManager.TYPE_WIFI) {
                    result = true
                } else if (type == ConnectivityManager.TYPE_MOBILE) {
                    result = true
                }
            }
        }
        return result
    }
}

interface OnNetworkChanges {
    /**
     * 网络变化
     * networkStatus: 网络连接状态
     * networkType:网络类型
     * networkChanges:网络变化
     */
    fun onNetworkChangesList(
        networkStatus: NetworkStatus?,
        networkType: NetworkType?,
        networkChanges: NetworkChanges?
    )
}